package com.rxyb.data.service.logic;

import cn.hutool.json.JSONUtil;
import com.rxyb.core.constants.DbConstants;
import com.rxyb.data.db.dao.RecordReqLogMapper;
import com.rxyb.data.db.po.RecordReqLog;
import com.rxyb.toolize.date.DateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author YJH
 * @version 1.0
 * @description 日志操作
 * @date 2020/6/19 18:18
 */
@Component
@Slf4j
public class RecordReqLogLogic {

    private final RecordReqLogMapper recordReqLogMapper;

    public RecordReqLogLogic(RecordReqLogMapper recordReqLogMapper) {
        this.recordReqLogMapper = recordReqLogMapper;
    }

    /**
     * 增加
     *
     * @param record
     * @return
     */
    public Optional<Integer> add(RecordReqLog record) {
        record.setAddTime(DateTime.getDateTime());
        record.setRowStatus(DbConstants.ROW_STATE_VALID);
        record.setUpdateTime(DateTime.getDateTime());
        int insert = recordReqLogMapper.insert(record);
        if (insert == 0) {
            log.error("record_req_log ---> add 增加记录失败[{}]", JSONUtil.toJsonStr(record));
            return Optional.empty();
        }
        log.error("record_req_log ---> add 增加记录成功[{}]", JSONUtil.toJsonStr(record));
        return Optional.of(insert);
    }
}
